﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HRWEB.Models
{
    public class EmployeeModel
    {
        public TEmployee Employee { get; set; }
        private int age;
        private hrdbEntities ent = new hrdbEntities();
        public int Age
        {
            get { if (Employee.FBirthday.HasValue) return DateTime.Now.Year - Employee.FBirthday.Value.Year; else return 0; }
            set { age = value; }
        }
        private string birthday;

        public string Birthday
        {
            get { return Employee.FBirthday.HasValue?Employee.FBirthday.Value.ToString("yyyy-MM-dd"):""; }
            set { birthday = value; }
        }
        private string party;

        public string Party
        {
            get { return Employee.TDictParty!=null?Employee.TDictParty.FName:""; }
            set { party = value; }
        }
        private string nation;

        public string Nation
        {
            get { return Employee.TDictNation!=null?Employee.TDictNation.FName:""; }
            set { nation = value; }
        }
        private string marriage;

        public string Marriage
        {
            get { return Employee.TDictMarriage!=null?Employee.TDictMarriage.FName:""; }
            set { Employee.FMarriageCode = value; }
        }
        private SelectList marriageList;

        public SelectList MarriageList
        {
            get { return new SelectList(ent.TDictMarriages.ToList(),"FMarriageCode","FName",Employee.FGenderCode); }
        }
        private string joinPartyDate;

        public string JoinPartyDate
        {
            get { return Employee.FJoinPartyDate.HasValue ? Employee.FJoinPartyDate.Value.ToString("yyyy-MM-dd") : ""; }
            set { joinPartyDate = value; }
        }
        //private string employeeID;
        //[HiddenInput(DisplayValue=false)]
        //public string EmployeeID
        //{
        //    get { return Employee.FEmployeeID; }
        //    set { Employee.FEmployeeID = value; }
        //}
        private string publicKey;
        [HiddenInput(DisplayValue=false)]
        public string PublicKey
        {
            get { return Employee.FPublicID; }
            set { Employee.FPublicID = value; }
        }
        private byte[] photo;

        public byte[] Photo
        {
            get
            {
                return Employee.FPhoto.Data; 
            }
            set { Employee.FPhoto.Data = value; }
        }

        private string photoMIME;

        public string PhotoMIME
        {
            get { return Employee.FPhoto.MIME; }
            set { Employee.FPhoto.MIME = value; }
        }
        private string belongOrgan;

        public string BelongOrgan
        {
            get
            {
                if (Employee.FBelongOrganCode == null)
                {
                    return "未知";
                }
                else if (Employee.FBelongOrganCode == "09")
                {
                    return "外聘";
                }
                else if (Employee.FBelongOrganCode == "10")
                {
                    return "借调";
                }
                string code = Employee.FBelongOrganCode.Substring(0, 4);
                switch (code)
                {
                    case "0101":
                        return "省编";
                    case "0102":
                        return "合同";
                    case "0201":
                        return "黄岛编制";
                    case "0202":
                        return "黄岛合同";
                    case "0301":
                        return "医学院";
                    case "0302":
                        return "心血管医院";
                    case "0401":
                        return "善达公司";
                    case "0402":
                        return "善达合同";
                    default:
                        return "未知";
                }
            }
            set { Employee.FBelongOrganCode = value; }
        }
        private int workedYears;

        public int WorkedYears
        {
            get { return Employee.FFirstWorkDate.HasValue ? DateTime.Now.Year - Employee.FFirstWorkDate.Value.Year : 0; }
            set { workedYears = value; }
        }

        private int intoHisYears;
        public int IntoHisYears
        {
            get { return Employee.FIntoHisDate.HasValue ? DateTime.Now.Year - Employee.FIntoHisDate.Value.Year : 0; }
            set { intoHisYears = value; }
        }
        public EmployeeModel(string publicKey)
        {
            var s_emp = from emp in ent.TEmployees where emp.FPublicID == publicKey select emp;
            if (s_emp.Any())
            {
                Employee = s_emp.Single();
            }
        }
        public EmployeeModel()
        {
        }

        public void Save(){
            ent.SaveChanges();
        }

    }
}